package Frames;

import Controllers.Reservation_Controller;
import Controllers.Reservation_Validator;
import DAO.Reservation_Dao;
import javax.swing.JOptionPane;

public class Reservation_Frame extends javax.swing.JFrame {

    public Reservation_Frame() {
        initComponents();
        messageLabel.setVisible(false);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        dateLabel = new javax.swing.JLabel();
        jDate = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        jSeatsNumber = new javax.swing.JTextField();
        jLastName = new javax.swing.JTextField();
        phoneNumberLabel = new javax.swing.JLabel();
        seatsNumberLabel = new javax.swing.JLabel();
        jName = new javax.swing.JTextField();
        jPhoneNumber = new javax.swing.JTextField();
        submitButton = new javax.swing.JButton();
        homeButton = new javax.swing.JButton();
        messageLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Booking");
        setBounds(new java.awt.Rectangle(200, 200, 0, 0));
        setPreferredSize(null);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Reservation", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 0, 36), new java.awt.Color(0, 0, 102))); // NOI18N

        dateLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        dateLabel.setText("Date");

        jDate.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jDateFocusGained(evt);
            }
        });

        nameLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        nameLabel.setText("Name");

        lastNameLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        lastNameLabel.setText("LastName");

        jSeatsNumber.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jSeatsNumberFocusGained(evt);
            }
        });

        jLastName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jLastNameFocusGained(evt);
            }
        });

        phoneNumberLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        phoneNumberLabel.setText("PhoneNumber");

        seatsNumberLabel.setFont(new java.awt.Font("Comic Sans MS", 0, 18)); // NOI18N
        seatsNumberLabel.setText("SeatsNumber");

        jName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jNameFocusGained(evt);
            }
        });

        jPhoneNumber.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPhoneNumberFocusGained(evt);
            }
        });

        submitButton.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        homeButton.setFont(new java.awt.Font("Comic Sans MS", 0, 12)); // NOI18N
        homeButton.setText("Home");
        homeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeButtonActionPerformed(evt);
            }
        });

        messageLabel.setFont(new java.awt.Font("Andalus", 0, 14)); // NOI18N
        messageLabel.setForeground(new java.awt.Color(255, 0, 0));
        messageLabel.setText("YOU TYPED SOMETHING WRONG , TRY AGAIN");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lastNameLabel)
                    .addComponent(phoneNumberLabel)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(11, 11, 11)
                                        .addComponent(nameLabel))
                                    .addComponent(seatsNumberLabel)
                                    .addComponent(dateLabel))
                                .addGap(151, 151, 151)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLastName)
                                    .addComponent(jName)
                                    .addComponent(jPhoneNumber)
                                    .addComponent(jSeatsNumber)
                                    .addComponent(jDate, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addComponent(messageLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(submitButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(homeButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(jName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameLabel)
                    .addComponent(jLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phoneNumberLabel)
                    .addComponent(jPhoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(seatsNumberLabel)
                    .addComponent(jSeatsNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dateLabel)
                    .addComponent(jDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(submitButton)
                    .addComponent(homeButton)
                    .addComponent(messageLabel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeButtonActionPerformed
        this.dispose();
        MainFrame enabler = new MainFrame();
        enabler.setVisible(true);
    }//GEN-LAST:event_homeButtonActionPerformed

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        Reservation_Controller controller = new Reservation_Controller(new Reservation_Dao());
        Reservation_Validator validator = new Reservation_Validator();
        try {
            boolean reservation = validator.validateReservation(jName.getText(), jLastName.getText(), jPhoneNumber.getText(), jSeatsNumber.getText(), jDate.getText());
            if (reservation == true) {

                controller.controlReservation(jName.getText(), jLastName.getText(), jPhoneNumber.getText(), jSeatsNumber.getText(), jDate.getText());
                this.dispose();
                MainFrame enabler = new MainFrame();
                enabler.setVisible(true);
            } else {
                messageLabel.setVisible(true);

            }
        } catch (IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, "Error in Reservation_Frame");
        }
    }//GEN-LAST:event_submitButtonActionPerformed

    private void jNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jNameFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jNameFocusGained

    private void jLastNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jLastNameFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jLastNameFocusGained

    private void jPhoneNumberFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPhoneNumberFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jPhoneNumberFocusGained

    private void jSeatsNumberFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jSeatsNumberFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jSeatsNumberFocusGained

    private void jDateFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jDateFocusGained
        messageLabel.setVisible(false);
    }//GEN-LAST:event_jDateFocusGained

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Personal_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Reservation_Frame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel dateLabel;
    private javax.swing.JButton homeButton;
    private javax.swing.JTextField jDate;
    private javax.swing.JTextField jLastName;
    private javax.swing.JTextField jName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jPhoneNumber;
    private javax.swing.JTextField jSeatsNumber;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel phoneNumberLabel;
    private javax.swing.JLabel seatsNumberLabel;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables
}
